package com.xandone.wcdog.controller;

import com.xandone.wcdog.config.Config;
import com.xandone.wcdog.mapper.VideoMapper;
import com.xandone.wcdog.pojo.Base.BaseListResult;
import com.xandone.wcdog.pojo.Base.BaseResult;
import com.xandone.wcdog.pojo.*;
import com.xandone.wcdog.service.FlowService;

import com.xandone.wcdog.service.UserService;
import com.xandone.wcdog.service.VideoService;
import com.xandone.wcdog.utils.IDUtils;
import com.xandone.wcdog.utils.ResultHelper;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.xandone.wcdog.config.Config.SUCCESS_CODE;

/**
 * @author ：xandone
 * created on  ：2019/1/15 19:26
 * description：
 */
@Controller
@RequestMapping("/video")
public class VideoController {
    @Autowired
    VideoService videoService;
    @Autowired
    UserService userService;
    @Autowired
    FlowService flowService;
    @Autowired
    VideoMapper videoMapper;

    @RequestMapping(value = "/addVideo", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult addVideo(@RequestBody Map<String, String> map) {
        BaseResult baseResult = new BaseResult();
        try {
            UserBean user = userService.getUserById(map.get("videoUserId"));
            if (user == null) {
                return ResultHelper.getResult(Config.ERROR_CODE);
            }
            if (user.getBanned() == 1) {
                return ResultHelper.getResult(Config.ERROR_BANNED_CODE);
            }
            VideoBean videoBean = videoService.addVideo(map);
            List<VideoBean> list = new ArrayList<>();
            list.add(videoBean);
            baseResult.setData(list);
            baseResult.setCode(SUCCESS_CODE);

            FlowBean flowBean = flowService.getFlowData(map.get(Config.ADMIN_ID));
            if (flowBean == null) {
                flowBean = new FlowBean();
                flowBean.setPostTime(new Date());
                flowService.addFlow(flowBean);
            }
            flowService.upDateFlow(flowBean);
        } catch (
                Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            return baseResult;
        }

        return baseResult;
    }

    @RequestMapping(value = "/videoList")
    @ResponseBody
    public BaseListResult getAllVideo(@RequestParam(value = "page") Integer page,
                                      @RequestParam(value = "row") Integer row,
                                      Integer tag) {
        BaseListResult baseResult = new BaseListResult();
        try {
            BaseListResult result = videoService.getAllVideo(page, row, tag);
            if (result != null) {
                result.setCode(SUCCESS_CODE);
                result.setMsg(Config.MES_REQUEST_SUCCESS);
                return result;
            }
            baseResult.setCode(Config.ERROR_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg(Config.MES_SERVER_ERROR);
        }
        return baseResult;
    }

    @RequestMapping(value = "/comment/addVideoComment", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult addComment(@RequestParam(value = "videoId") String videoId,
                                 @RequestParam(value = "userId") String userId,
                                 @RequestParam(value = "details") String details) {
        BaseResult baseResult = new BaseResult();
        try {
            UserBean user = userService.getUserById(userId);
            if (user == null) {
                return ResultHelper.getResult(Config.ERROR_CODE);
            }
            if (user.getBanned() == 1) {
                return ResultHelper.getResult(Config.ERROR_BANNED_CODE);
            }
            List<VideoCommentBean> dataList = new ArrayList<>();

            if (TextUtils.isEmpty(userId)) {
                baseResult.setCode(Config.ERROR_CODE);
                return baseResult;
            }
            VideoCommentBean commentBean = new VideoCommentBean();
            commentBean.setCommentId(IDUtils.RandomId());
            commentBean.setVideoId(videoId);
            commentBean.setCommentUserId(userId);
            commentBean.setCommentDetails(details);
            commentBean.setCommentDate(new Date());
            commentBean.setCommentNick(user.getNickname());
            commentBean.setCommentIcon(user.getUserIcon());
            videoService.addVideoComment(commentBean);
            baseResult.setCode(SUCCESS_CODE);
            dataList.add(commentBean);
            baseResult.setData(dataList);
            return baseResult;
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            return baseResult;
        }
    }

    @RequestMapping(value = "/comment/list")
    @ResponseBody
    public BaseListResult getAllVideoCommentById(@RequestParam(value = "page") Integer page,
                                                @RequestParam(value = "row") Integer row,
                                                @RequestParam(value = "videoId") String videoId) throws Exception {
        BaseListResult baseResult = new BaseListResult();
        try {
            BaseListResult result = videoService.getAllVideoCommentById(page, row, videoId);
            if (result != null) {
                result.setCode(SUCCESS_CODE);
                result.setMsg(Config.MES_REQUEST_SUCCESS);
                return result;
            }
            baseResult.setCode(Config.ERROR_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg(Config.MES_SERVER_ERROR);
        }
        return baseResult;
    }


    @RequestMapping(value = "video/thumbs", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult thumbsVideo(@RequestParam(value = "videoId") String videoId,
                                 @RequestParam(value = "videoUserId") String videoUserId) throws Exception {
        BaseResult baseResult = new BaseResult();
        boolean isThumbs = false;
        List<VideoLikeBean> likeBeans = videoService.selectVideoLikeById(videoId);
        for (int i = 0; i < likeBeans.size(); i++) {
            if (videoUserId.equals(likeBeans.get(i).getVideoUserId())) {
                // 已点赞
                isThumbs = true;
                break;
            }
        }
        if (isThumbs) {
            // 已点赞
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg("你已经点过赞了");
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("video_id", videoId);
            map.put("art_like_count", 1);
            videoService.changeVideoLikeCount(map);
            videoService.thumbsVideo(videoId, videoUserId);
            baseResult.setCode(SUCCESS_CODE);
        }

        return baseResult;
    }

    @RequestMapping(value = "video/collects", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult collectsVideo(@RequestParam(value = "videoId") String videoId,
                                 @RequestParam(value = "videoUserId") String videoUserId) throws Exception {
        BaseResult baseResult = new BaseResult();
        boolean isThumbs = false;
        List<VideoCollectBean> likeBeans = videoMapper.selectVideoCollectBeanById(videoId);
        for (int i = 0; i < likeBeans.size(); i++) {
            if (videoUserId.equals(likeBeans.get(i).getVideoUserId())) {
                // 已收藏
                isThumbs = true;
                break;
            }
        }
        if (isThumbs) {
            // 已收藏
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg("你已经收藏过了");
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("video_id", videoId);
            map.put("art_collect_count", 1);
            videoService.changeVideoCollectCount(map);
            videoService.collectsVideo(videoId, videoUserId);
            baseResult.setCode(SUCCESS_CODE);
        }

        return baseResult;
    }

    @RequestMapping(value = "/videoDetails", method = RequestMethod.POST)
    @ResponseBody
    public BaseResult deleteVideoByList(@RequestParam(value = "videoId") String videoId) {
        BaseResult baseResult = new BaseResult();
        try {
            VideoBean videoBean = videoService.getVideoById(videoId);
            List<VideoBean> list = new ArrayList<>();
            list.add(videoBean);
            baseResult.setData(list);
            baseResult.setCode(Config.SUCCESS_CODE);
            baseResult.setMsg("获取数据成功");
            return baseResult;
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg("获取数据异常");
        }
        return baseResult;
    }

    @RequestMapping("/videoList/foggy")
    @ResponseBody
    public BaseListResult getVideoListFog(@RequestParam(value = "page") Integer page,
                                         @RequestParam(value = "row") Integer row,
                                         String key) {
        BaseListResult baseResult = new BaseListResult();
        try {
            BaseListResult result = videoService.getVideoListFog(page, row, key);
            if (result != null) {
                result.setCode(SUCCESS_CODE);
                result.setMsg(Config.MES_REQUEST_SUCCESS);
                return result;
            }
            baseResult.setCode(Config.ERROR_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg(Config.MES_SERVER_ERROR);
        }
        return baseResult;
    }

    @RequestMapping(value = "video/thumbs/self")
    @ResponseBody
    public BaseResult getThumbsVideo(String videoId, String videoUserId) throws Exception {
        BaseResult baseResult = new BaseResult();
        boolean isThumbs = false;

        VideoBean videoBean = videoService.getVideoById(videoId);
        List<VideoBean> data = new ArrayList<>();
        data.add(videoBean);

        List<VideoLikeBean> likeBeans = videoService.selectVideoLikeById(videoId);
        for (int i = 0; i < likeBeans.size(); i++) {
            if (videoUserId.equals(likeBeans.get(i).getVideoUserId())) {
                // 已点赞
                isThumbs = true;
                break;
            }
        }
        if (isThumbs) {
            // 已点赞
            baseResult.setCode(Config.ERROR_CODE);
        } else {
            baseResult.setCode(SUCCESS_CODE);
        }
        baseResult.setData(data);
        return baseResult;
    }


    @RequestMapping( "video/collects/self")
    @ResponseBody
    public BaseResult getCollectsVideo(String videoId, String videoUserId) throws Exception {
        BaseResult baseResult = new BaseResult();
        boolean isThumbs = false;

        VideoBean videoBean = videoService.getVideoById(videoId);
        List<VideoBean> data = new ArrayList<>();
        data.add(videoBean);

        List<VideoCollectBean> likeBeans = videoMapper.selectVideoCollectByUserId(videoId);
        for (int i = 0; i < likeBeans.size(); i++) {
            if (videoUserId.equals(likeBeans.get(i).getVideoUserId())) {
                // 已收藏
                isThumbs = true;
                break;
            }
        }
        if (isThumbs) {
            // 已收藏
            baseResult.setCode(Config.ERROR_CODE);
        } else {
            baseResult.setCode(SUCCESS_CODE);
        }
        baseResult.setData(data);
        return baseResult;
    }

    @RequestMapping(value = "/videoList/search")
    @ResponseBody
    public BaseListResult searchVideoList(@RequestParam(value = "page") Integer page,
                                          @RequestParam(value = "row") Integer row,
                                          String key,
                                          String videoId,
                                          String videoUserId,
                                          String category,
                                          String tags) {
        BaseListResult baseResult = new BaseListResult();
        try {
            VideoBean videoBean = new VideoBean(key, videoId, videoUserId, category, tags);
            BaseListResult result = videoService.searchVideoList(page, row, videoBean);
            if (result != null) {
                result.setCode(SUCCESS_CODE);
                result.setMsg(Config.MES_REQUEST_SUCCESS);
                return result;
            }
            baseResult.setCode(Config.ERROR_CODE);
        } catch (Exception e) {
            e.printStackTrace();
            baseResult.setCode(Config.ERROR_CODE);
            baseResult.setMsg(Config.MES_SERVER_ERROR);
        }
        return baseResult;
    }

}
